const { data } = require("uview-ui/libs/mixin/mixin");

const GET = "GET";
const POST = "POST";

const baseURL = "https://songcan.xyct1688.com/"; //配置的域名

// loding白名单
const whiteList = [
  "weapp/api/weapp/authcodetoopenid",
  "api/tripartite/mobileauth",
];
// 用来储存发送了几个请求
let ajaxTimes = 0;

function request(method, url, data = {}) {
  if (whiteList.indexOf(url) == -1) {
    // 每发送一次请求就让ajaxTimes++
    ajaxTimes++;
    //请求的时候加载一个会转圈圈的图标
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
  }
  function modal(msg) {
    uni.showModal({
      title: msg,
      content: "",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          uni.removeStorageSync("token");
          uni.navigateTo({
            url: "/pages_login/login/login",
          });
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  }
  return new Promise(function (resolve, reject) {
    let header = {
      "content-type": "application/json",
      // 'content-type': 'multipart/form-data',
      // 'content-type': 'application/x-www-form-urlencoded;application/json',
      // content-type application/x-www-form-urlencoded;application/json
      // application/x-www-form-urlencoded;application/json;form-data
    };
    data.app_type = "weapp";
    data.app_type_name = "微信小程序";
    if (uni.getStorageSync("token")) {
      data.token = uni.getStorageSync("token");
    }

    uni.request({
      url: baseURL + url,
      method: method,
      data: method === POST ? JSON.stringify(data) : data,
      header: header,
      success(res) {
        //请求成功
        //判断code是否为0表示成功
        if (res.data.code == 0) {
          resolve(res);
        } else {
          if (res.data.message == "token已过期") {
            modal("登录已过期，是否重新登录？");
          }
          if (res.data.message == "token不存在") {
            modal("是否登录体验？");
          } else {
            uni.showToast({
              title: res.data.message,
              icon: "none",
              duration: 2000,
            });
            // 其他错误
            reject(res.data.message);
          }
        }
      },
      fail(err) {
        //请求失败
        reject(err);
      },
      // 请求成功与否都会执行的回调
      complete() {
        // 每次请求完成后就让ajaxTimes--
        ajaxTimes--;
        // 当ajaxTimes===0的时候 也就是所有的请求都结束的时候关闭转圈圈
        if (ajaxTimes <= 0) {
          uni.hideLoading();
        }
      },
    });
  });
}
function requests(method, url, data) {
  return new Promise(function (resolve, reject) {
    let header = {
      "content-type": "application/json",
    };
    data.app_type = "weapp";
    data.app_type_name = "微信小程序";
    if (uni.getStorageSync("token")) {
      data.token = uni.getStorageSync("token");
    }

    uni.request({
      url: baseURL + url,
      method: method,
      data: method === POST ? JSON.stringify(data) : data,
      header: header,
      success(res) {
        //请求成功
        resolve(res);
      },
      fail(err) {
        //请求失败
        reject(err);
      },
    });
  });
}

//接口可以集合到一块，集中管理
const API = {
  // 用户中心
  // 账户信息
  postAccountInformation: (data) =>
    request(POST, "api/memberaccount/info", data),
  // 账户明细
  postAccountDetails: (data) => request(POST, "api/memberaccount/page", data),
  // 获取openid
  postOpenId: (data) => request(POST, "weapp/api/weapp/authcodetoopenid", data),
  // 手机号授权登录
  postPhoneLogin: (data) => request(POST, "api/tripartite/mobileauth", data),
  // 用户信息
  postUserInformation: (data) => request(POST, "api/member/info", data),
  // 反馈类型
  postFeedbackType: (data) => request(POST, "api/member/feedbacktype", data),
  // 反馈图片上传	pages/my/SystemEntry/Feedback
  // 反馈提交
  postFeedbackSubmitTo: (data) => request(POST, "api/member/feedback", data),
  // 头像上传
  postPictureUpload: (data) => request(POST, "api/upload/headimg", data),
  // 修改头像
  postModifyheadimg: (data) => request(POST, "api/member/modifyheadimg", data),
  // 修改昵称
  postNickname: (data) => request(POST, "api/member/modifynickname", data),
  // 修改性别
  postSex: (data) => request(POST, "api/member/modifysex", data),
  // 修改生日
  postBirthday: (data) => request(POST, "api/member/modifybirthday", data),
  // 分享码
  postSharingCode: (data) => request(POST, "api/member/qrcode", data),
  // 商户申请入驻
  postMerchantApplicationForEntry: (data) =>
    request(POST, "api/member/shopApply", data),
  // 申请商户需要的信息入驻协议等
  postAgreement: (data) => request(POST, "api/member/shopApplySetting", data),
  // 商户入驻申请状态
  postMerchantStatus: (data) =>
    request(POST, "api/member/shopApplyStatus", data),
  // 商户入驻验证码
  postMerchantCode: (data) => request(POST, "api/member/shopapplycode", data),
  // 商户入驻申请信息
  postMerchantInformation: (data) =>
    request(POST, "api/member/shopApplyInfo", data),
  // 邀请记录
  postInvitationRecord: (data) => request(POST, "api/member/team", data),

  // 首页
  // 轮播图
  postSwiper: (data) => request(POST, "api/banner/lists", data),
  // 店铺列表
  postStoreList: (data) => request(POST, "api/shop/page", data),
  // 店铺分类列表?site_id=2
  postStoreClassification: (data) =>
    request(POST, "api/shopgoodscategory/tree", data),
  // 店铺详情
  postShopDetails: (data) => request(POST, "api/shop/info", data),
  // 商品列表
  postCommodityList: (data) => request(POST, "api/goodssku/page", data),
  // 商品详情
  postCommodityDetails: (data) => request(POST, "api/goodssku/detail", data),
  // 选择学校
  postSelectSchool: (data) => request(POST, "api/store/page", data),
  // 收藏店铺
  postCollectShop: (data) => request(POST, "api/shopmember/add", data),
  // 取消收藏店铺
  postCancellationCollectShop: (data) =>
    request(POST, "api/shopmember/delete", data),
  // 收藏店铺列表
  postCollectShopList: (data) =>
    request(POST, "api/shopmember/membershoppages", data),
  // 检测该店铺是否关注
  postShopWhetherToFollow: (data) =>
    request(POST, "api/shopmember/issubscribe", data),
  // 评价数量
  postShopEvaluate: (data) =>
    request(POST, "api/goodsevaluate/imagecount", data),
  // 评价列表
  postShopEvaluateList: (data) => request(POST, "api/goodsevaluate/page", data),
  // 店铺满减
  postStoreFullReduction: (data) =>
    request(POST, "manjian/api/manjian/shopManjian", data),
  // 省市区列表
  postProvincialAndUrbanAreasList: (data) =>
    request(POST, "/api/address/lists", data),
  // 导航
  postNav: (data) => request(POST, "api/nav/lists", data),
  // 消息列表
  postMessageList: (data) =>
    request(
      POST,
      "sitemessage/api/sitemessage/getSiteMemberSubMessageGroupList",
      data
    ),
  // 消息详情
  postMessageDetails: (data) =>
    request(
      POST,
      "sitemessage/api/sitemessage/getSiteMessageRecordsInfo",
      data
    ),
  // 门店配送配置
  postStoreDeliveryConfiguration: (data) =>
    request(POST, "api/shop/express", data),

  // 用户地址
  // 添加地址
  postAddress: (data) => request(POST, "api/memberaddress/add", data),
  // 地址列表
  postAddressList: (data) => request(POST, "api/memberaddress/page", data),
  // 编辑地址
  postAddressEdit: (data) => request(POST, "api/memberaddress/edit", data),
  // 删除地址
  postAddressDel: (data) => request(POST, "api/memberaddress/delete", data),
  // 设置默认地址
  postSetUpDefault: (data) =>
    request(POST, "api/memberaddress/setdefault", data),
  // 地址信息
  postAddressInformation: (data) =>
    request(POST, "api/memberaddress/info", data),
  // 根据经纬度获取省市区
  postLatitudeAndLongitud: (data) =>
    request(POST, "api/memberaddress/tranAddressInfo", data),

  // 拼团
  // 商品列表
  postGroupProductList: (data) => request(POST, "pintuan/api/goods/page", data),
  // 商品详情
  postProductDetailsOfTheGroup: (data) =>
    request(POST, "pintuan/api/goods/detail", data),
  // 商品规格
  postSpecifications: (data) => request(POST, "pintuan/api/goods/info", data),
  // 发起拼团
  postInitiateAGroupGormation: (data) =>
    request(POST, "pintuan/api/pintuangroup/lists", data),
  // 拼团订单提交
  postGroupOrderSubmission: (data) =>
    requests(POST, "pintuan/api/ordercreate/create", data),

  // 秒杀
  // 当天可秒杀的时间段
  postFlashKillTimePeriod: (data) =>
    request(POST, "seckill/api/seckill/lists", data),
  // 商品列表
  postFlashKilling: (data) =>
    request(POST, "seckill/api/seckillgoods/page", data),
  // 商品详情
  postFlashKillingDetails: (data) =>
    request(POST, "seckill/api/seckillgoods/detail", data),
  // 商品规格
  postFlashKillSpecifications: (data) =>
    request(POST, "seckill/api/seckillgoods/info", data),
  // 秒杀订单提交
  postFlashKillingOrderSubmission: (data) =>
    requests(POST, "seckill/api/ordercreate/create", data),

  // 购物车
  // 加入购物车
  postAddShoppingCart: (data) => request(POST, "api/cart/add", data),
  // 购物车数量
  postShoppingCartNum: (data) => request(POST, "api/cart/count", data),
  // 购物车列表
  postShoppingCartList: (data) => request(POST, "api/cart/goodsLists", data),
  // 购物车金额
  postShoppingCartAmountOfMoney: (data) =>
    request(POST, "api/cart/money", data),
  // 购物车删除
  postShoppingCartDel: (data) => request(POST, "api/cart/delete", data),
  // 购物车编辑
  postShoppingCartEdit: (data) => request(POST, "api/cart/edit", data),

  // 支付
  postPayment: (data) => request(POST, "api/pay/pay", data),

  // 订单
  // 订单提交页计算信息
  postOrderGoodsPageCount: (data) =>
    request(POST, "api/ordercreate/calculate", data),
  // 待支付订单数据初始化
  postInitializationOfPendingOrderData: (data) =>
    request(POST, "api/ordercreate/payment", data),
  // 订单提交
  postOrderGoodsSubmitTo: (data) =>
    request(POST, "api/ordercreate/create", data),
  // 订单支付
  postOrderGoodsPayment: (data) => request(POST, "api/pay/pay", data),
  // 余额支付
  checkpaypassword: (data) =>
    request(POST, "api/member/checkpaypassword", data),

  // 我的订单
  postOrderGoods: (data) => request(POST, "api/order/lists", data),
  // 订单详情
  postOrderDetails: (data) => request(POST, "api/order/detail", data),
  // 评价订单页展示信息
  postEvaluativeInformation: (data) =>
    request(POST, "api/order/evluateinfo", data),
  // 配送员评价词
  postDeliveryMetaEvaluationTerms: (data) =>
    request(POST, "api/deliveryman/evaluateword", data),
  // 评价订单提交未完成
  postEvaluationSubmission: (data) =>
    request(POST, "api/goodsevaluate/add", data),

  // 优惠券
  // 优惠券（总平台）
  postCouponAssemble: (data) =>
    request(POST, "platformcoupon/api/platformcoupon/memberpage", data),
  // 我的优惠券（店铺）
  postCouponShop: (data) => request(POST, "coupon/api/coupon/memberpage", data),
  // 店铺优惠券列表
  postCouponList: (data) =>
    request(POST, "coupon/api/coupon/typepagelists", data),
  // 领取店铺优惠券
  postReceiveShopCoupon: (data) =>
    requests(POST, "coupon/api/coupon/receive", data),
  // 检测是都可以领取店铺优惠券
  postTestingCanCollectStoreCoupons: (data) =>
    request(POST, "coupon/api/coupon/receivedNum", data),
  // 平台会员优惠券列表
  postPlatformMemberCouponList: (data) =>
    request(POST, "platformcoupon/api/platformcoupon/typepagelists", data),
  // 领取平台会员优惠券
  postPlatformMemberCoupon: (data) =>
    request(POST, "platformcoupon/api/platformcoupon/receive", data),
  // 平台优惠券信息
  postPlatformCouponInformation: (data) =>
    request(POST, "platformcoupon/api/platformcoupon/typeinfo", data),
  // 会员卡优惠券兑换店铺的膨胀优惠券
  postMembershipArdCouponStore: (data) =>
    request(POST, "platformcoupon/api/platformcoupon/exchangeshop", data),

  // 积分
  // 可兑换列表
  postIntegralList: (data) =>
    request(POST, "pointexchange/api/goods/page", data),
  // 兑换记录
  postIntegralRecord: (data) =>
    request(POST, "pointexchange/api/order/page", data),
  // 兑换商品提交订单
  postCommoditySubmitToOrderForGoods: (data) =>
    request(POST, "pointexchange/api/ordercreate/create", data),
  // 积分兑换规则
  postPointsRedemptionRules: (data) =>
    request(POST, "pointexchange/api/goods/rule", data),

  // 会员卡
  // 获取会员卡信息
  postMemberInformation: (data) =>
    request(POST, "supermember/api/membercard/info", data),
  // 获取会员卡信息
  membercardMember_info: (data) =>
    request(POST, "supermember/api/membercard/member_info", data),
  // 会员卡订单提交
  postMemberOrderSubmission: (data) =>
    request(POST, "supermember/api/ordercreate/create", data),

  // 签到
  postSignIn: (data) => request(POST, "api/membersignin/signin", data),
  // 签到规则
  postCheckInRules: (data) => request(POST, "api/membersignin/award", data),
  // 判断当天是否签到
  postsignCheckInOrNot: (data) =>
    request(POST, "api/membersignin/issign", data),
  // 签到设置
  postSignInSetUp: (data) =>
    request(POST, "api/membersignin/getSignStatus", data),
  // 签到记录
  postSignInRecord: (data) =>
    request(POST, "api/membersignin/getSignRecords", data),

  // 派送员
  // 配送员申请入驻
  postDeliveryPersonnelApplyForEntry: (data) =>
    requests(POST, "api/deliveryman/apply", data),
  // 派送订单
  postDeliveryOrder: (data) => request(POST, "api/deliveryman/order", data),
  // 接单大厅
  postReceivingHall: (data) =>
    request(POST, "api/deliveryman/orderReceiving", data),
  // 订单详情
  postOrderGoodsDetails: (data) =>
    request(POST, "api/deliveryman/detail", data),
  // 送达
  postService: (data) => request(POST, "api/deliveryman/takeDelivery", data),
  // 取货
  postPickUp: (data) => request(POST, "api/deliveryman/pick", data),
  // 配送员信息
  postShipperInformation: (data) => request(POST, "api/deliveryman/info", data),
  // 配送员评价关键词数量
  postNumberOfEvaluationWords: (data) =>
    request(POST, "api/deliveryman/evaluateWordCount", data),
  // 配送员评价列表
  postEvaluateList: (data) =>
    request(POST, "api/deliveryman/evaluateList", data),
  // 提现
  postWithdrawal: (data) => request(POST, "api/memberwithdraw/apply", data),
  // 提现设置
  postWithdrawalSetUp: (data) =>
    request(POST, "api/memberwithdraw/config", data),
  // 配送员订单统计
  postOrderStatistics: (data) => request(POST, "api/deliveryman/stat", data),
  // 配送员申请入驻验证码
  postRiderCode: (data) => request(POST, "api/deliveryman/applycode", data),
  // 接单
  postReceivingOrders: (data) => request(POST, "api/deliveryman/jiedan", data),

  // 核销
  // 核销记录
  postWriteOffRecords: (data) => request(POST, "api/shopverify/records", data),
  // 核销信息
  postWriteOffInformation: (data) =>
    request(POST, "api/shopverify/verifyInfo", data),
  // 核销订单
  postWriteOffOrderGoods: (data) =>
    request(POST, "api/shopverify/verify", data),
  // 检测是否是核销员
  postVerifierOrNot: (data) =>
    requests(POST, "api/verify/checkIsverifier", data),

  // 充值
  // 充值套餐
  postRechargePackage: (data) =>
    request(POST, "memberrecharge/api/memberrecharge/page", data),
  // 检测用户是否设置支付密码
  postDoesTheUserSetAPaymentPassword: (data) =>
    request(POST, "api/member/issetpayaassword", data),
  // 修改支付密码发送验证码
  postRechargePasswordCode: (data) =>
    request(POST, "api/member/paypwdcode", data),
  // 设置/修改密码
  postSetUpDeitPassword: (data) =>
    request(POST, "api/member/modifypaypassword", data),
  // 充值提价
  postRechargeSubmission: (data) =>
    request(POST, "memberrecharge/api/ordercreate/create", data),

  //取消订单  //没支付关闭
  closeOrder: (data) => request(POST, "api/order/close", data),
  //取消订单原因
  closeOrderReason: (data) => request(POST, "api/order/reason_list", data),
  //取消订单-提交退款审核
  closeOrderToAuth: (data) => request(POST, "api/Orderrefund/refund", data),

  //职位发布
  jobAdd: (data) => request(POST, "api/xyt.job/add", data),
  //职位编辑
  jobEdit: (data) => request(POST, "api/xyt.job/edit/id/" + data.id),
  // 职位列表
  jobList: (data) => request(POST, "api/xyt.job/list", data),
  // 职位详情
  jobDetail: (data) => request(POST, "api/xyt.job/detail/id/" + data.id),
  // 职位删除
  jobdel: (data) => request(POST, "api/xyt.job/del/id/" + data.id),

  //回收发布
  recyclingAdd: (data) => request(POST, "api/xyt.recycling/add", data),
  //回收编辑
  recyclingEdit: (data) =>
    request(POST, "api/xyt.recycling/edit/id/" + data.id),
  // 回收列表
  recyclingList: (data) => request(POST, "api/xyt.recycling/list", data),
  // 回收详情
  recyclingDetail: (data) =>
    request(POST, "api/xyt.recycling/detail/id/" + data.id),
  // 回收删除
  recyclingdel: (data) => request(POST, "api/xyt.recycling/del/id/" + data.id),

  //二手发布
  secondhandAdd: (data) => request(POST, "api/xyt.secondhand/add", data),
  //二手编辑
  secondhandEdit: (data) =>
    request(POST, "api/xyt.secondhand/edit/id/" + data.id),
  // 二手列表
  secondhandList: (data) => request(POST, "api/xyt.secondhand/list", data),
  //二手详情
  secondhanddDtail: (data) =>
    request(POST, "api/xyt.secondhand/detail/id/" + data.id),
  // 二手删除
  secondhanddel: (data) =>
    request(POST, "api/xyt.secondhand/del/id/" + data.id),

  //失物发布
  lostfoundAdd: (data) => request(POST, "api/xyt.lostfound/add", data),
  //失物编辑
  lostfoundEdit: (data) =>
    request(POST, "api/xyt.lostfound/edit/id/" + data.id),
  // 失物列表
  lostfoundList: (data) => request(POST, "api/xyt.lostfound/list", data),
  //失物详情
  lostfoundDetail: (data) =>
    request(POST, "api/xyt.lostfound/detail/id/" + data.id),
  // 失物删除
  lostfounddel: (data) => request(POST, "api/xyt.lostfound/del/id/" + data.id),

  // 今日资讯列表
  informationList: (data) => request(POST, "api/xyt.information/list"),
  // 今日资讯点赞
  informationLike: (data) =>
    request(POST, "api/xyt.information/like/id/" + data.id),
  // 今日资讯详情
  informationDetail: (data) =>
    request(POST, "api/xyt.information/detail/id/" + data.id),
  // 今日资讯评论
  InformationCommentAdd: (data) =>
    request(POST, "api/xyt.InformationComment/add", data),
  // 今日资讯评论列表
  InformationCommentList: (data) =>
    request(POST, "api/xyt.InformationComment/list", data),
  // 校宇通轮播图
  bannerList: () => request(POST, "/api/xyt.banner/list"),

  // 跑腿新增
  placeOrder: (data) => request(POST, "pt/order/placeOrder", data),
  // 跑腿列表
  orderLists: (data) => request(POST, "pt/order/lists", data),
  // 跑腿接单列表
  legwork_order: (data) => request(POST, "pt/legwork_order/lists", data),
  // 跑腿接单
  legwork_orderReceive: (data) =>
    request(POST, "pt/legwork_order/receive", data),
  // 跑腿完成
  legwork_orderFinish: (data) => request(POST, "pt/legwork_order/finish", data),
  // 跑腿支付
  payPrepay: (data) => request(POST, "pt/pay/prepay", data),
  // 首页活动图片
  recommendRegister: (data) =>
    request(POST, "api/index/recommendRegister", data),
};
module.exports = {
  API: API,
  baseURL: baseURL,
};
